using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;
namespace Test {
	class MqAnalysis {
		static void MainX() {

			DAL local=new DAL("pon-wdsq10\\IN2012","ReportTest");
			//DAL local=new DAL();

			//local.NonQry("drop table MqStat");
			//local.NonQry("create table MqStat(id int identity,mgr varchar(20),que varchar(200),inserted datetime,tspent int)");
			DataTable tab=new DAL("pon-wpsq21-db09\\in09","callout").Query("select id,applic,calltext,callDateTime,Duration from call(nolock)where callDateTime>=getdate()-365*4 and callText like '%Current Q Depth%' and callText like '%Q DEPTH ALERT%Alert id%' order by id desc");
			Regex rx=new Regex("Object\\t?([^ ]+?:.+?)\\n");

			local.NonQry("truncate table MqStat");
			Stopwatch sw=Stopwatch.StartNew();

			foreach(DataRow row in tab.Rows)
			{
				string id=row["id"].ToString();
				string data=row["callText"].ToString();
				string duration=row["duration"].ToString();
				int iduration=0;
				if(duration.Length==4)
					iduration=int.Parse(duration.Substring(0,2))*60+int.Parse(duration.Substring(2,2));
				//string queue=new Regex("Object\\t?(.+?:.+?)\\n").Match(data).Groups[1].Value.Trim();
				MatchCollection matches=rx.Matches(data);
				foreach(Match mat in matches)
				{
					string queue=mat.Groups[1].Value.Trim();

					string[] arr=queue.Split(':');
					if(arr.Length==2&&!arr[0].Contains(" ")&&!arr[1].Contains(" "))
						local.NonQry("insert into MqStat(mgr,que,inserted,tspent)values('{0}','{1}',convert(datetime,'{2:yyyy-MM-dd HH:mm:ss}',20),{3})",arr[0],arr[1],(DateTime)row["callDateTime"],iduration);
				}

				//if(queue.Contains("Queue"))
				//  Debugger.Break();

			}
			Console.WriteLine(sw.Elapsed);
		}
	}
}
